#include "TCPServer.hpp"
#include <signal.h>
#include <iostream>

TCPServer* server = nullptr;

void signalHandler(int signal) {
    if (server) {
        std::cout << "\n正在关闭服务器..." << std::endl;
        server->Stop();
    }
    exit(0);
}

int main(int argc, char* argv[]) {
    // 设置信号处理
    signal(SIGINT, signalHandler);
    
    uint16_t port = kDefaultPort;
    std::string ip = "0.0.0.0";
    
    // 解析命令行参数
    for (int i = 1; i < argc; i++) {
        std::string arg = argv[i];
        
        if (arg == "--port" && i + 1 < argc) {
            port = static_cast<uint16_t>(std::atoi(argv[++i]));
        } else if (arg == "--bind" && i + 1 < argc) {
            ip = argv[++i];
        } else if (arg == "--help") {
            std::cout << "用法: " << argv[0] << " [选项]" << std::endl;
            std::cout << "选项:" << std::endl;
            std::cout << "  --port <端口>    指定监听端口 (默认: " << kDefaultPort << ")" << std::endl;
            std::cout << "  --bind <IP>      指定绑定IP (默认: 0.0.0.0)" << std::endl;
            std::cout << "  --help           显示此帮助信息" << std::endl;
            return 0;
        }
    }
    
    server = new TCPServer(port, ip);
    server->Start();

    delete server;
    return 0;
}